package com.jm.chat.utils.imagepicker;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jm.chat.R;

import java.util.List;

public class DialogAdapter extends BaseAdapter {
    private List<String> mStrings;
    private Viewholder viewholder;
    private LayoutInflater layoutInflater;
    private Activity mActivity;
    private boolean mUseCustomColor = false;
    private int mFirstItemColor;
    private int mOtherItemColor;

    public DialogAdapter(List<String> strings, Activity mActivity, boolean mUseCustomColor) {
        this.mStrings = strings;
        this.layoutInflater=mActivity.getLayoutInflater();
        this.mActivity = mActivity;
        this.mUseCustomColor = mUseCustomColor;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mStrings.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mStrings.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            viewholder=new Viewholder();
            convertView=layoutInflater.inflate(R.layout.view_dialog_item, null);
            viewholder.dialogItemButton=(TextView) convertView.findViewById(R.id.dialog_item_bt);
            convertView.setTag(viewholder);
        }else{
            viewholder=(Viewholder) convertView.getTag();
        }
        viewholder.dialogItemButton.setText(mStrings.get(position));
        if (!mUseCustomColor) {
            mFirstItemColor = mActivity.getResources().getColor(R.color.blue);
            mOtherItemColor = mActivity.getResources().getColor(R.color.blue);
        }
        if (1 == mStrings.size()) {
            viewholder.dialogItemButton.setTextColor(mFirstItemColor);
            viewholder.dialogItemButton.setBackgroundResource(R.drawable.dialog_item_bg_only);
        } else if (position == 0) {
            viewholder.dialogItemButton.setTextColor(mFirstItemColor);
            viewholder.dialogItemButton.setBackgroundResource(R.drawable.select_dialog_item_bg_top);
        } else if (position == mStrings.size() - 1) {
            viewholder.dialogItemButton.setTextColor(mOtherItemColor);
            viewholder.dialogItemButton.setBackgroundResource(R.drawable.select_dialog_item_bg_buttom);
        } else {
            viewholder.dialogItemButton.setTextColor(mOtherItemColor);
            viewholder.dialogItemButton.setBackgroundResource(R.drawable.select_dialog_item_bg_center);
        }
        return convertView;
    }

    public class Viewholder {
        public TextView dialogItemButton;
    }
}
